<template>
  <div>
    <!-- 新增客户 -->
    <el-dialog
      title="新增客户"
      :visible.sync="newClientdialogVisible"
      width="744px"
      :before-close="CancelClientSubmit"
    >
      <span>
        <div>
          <el-form
            ref="newClientForm"
            :rules="newClientRules"
            :model="newClientForm"
          >
            <el-row :gutter="40">
              <el-col :span="8">
                <el-form-item label="客户姓名" prop="name">
                  <el-input
                    class="w-100"
                    v-model="newClientForm.name"
                    placeholder="请输入客户姓名"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="客户手机号1" prop="mobile">
                  <el-input
                    class="w-100"
                    v-model="newClientForm.mobile"
                    placeholder="请输入客户手机号1"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="客户手机号2">
                  <el-input
                    class="w-100"
                    v-model="newClientForm.mobile2"
                    placeholder="请输入客户手机号2"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="消费密码" prop="password">
                  <el-input
                    class="w-100"
                    v-model="newClientForm.password"
                    placeholder="请输入6位消费密码"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="客户性别" prop="sex">
                  <el-select
                    v-model="newClientForm.sex"
                    placeholder="请选择"
                    class="w-100"
                  >
                    <el-option
                      v-for="item in Sexoptions"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="客户生日">
                  <el-date-picker
                    v-model="newClientForm.birthday"
                    type="date"
                    class="w-100"
                    placeholder="请选择客户生日"
                    value-format="yyyy-MM-dd"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="推荐员工">
                  <el-select
                    v-model="newClientForm.referrer"
                    placeholder="请选择推荐员工"
                    class="w-100"
                    :loading="storeUserLoading"
                  >
                    <el-option
                      v-for="item in userList"
                      :key="item.userId"
                      :label="item.nickname"
                      :value="item.userId"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="客户来源" prop="sourceId">
                  <el-select
                    v-model="newClientForm.sourceId"
                    placeholder="请选择客户来源"
                    class="w-100"
                  >
                    <el-option
                      v-for="item in ClientSourceData"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button class="cancel_btn" @click="CancelClientSubmit"
          >取 消</el-button
        >
        <el-button type="primary" @click="ClientSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  CreateClient,
  getClientSourceList,
  getStoreUser,
} from "@/api/business/BusinessIndex";
import { phoneTest } from "@/utils/regular";
export default {
  name: "EryuoaStoreFrontendNewClient",

  data() {
    return {
      newClientdialogVisible: false, // 新增客户
      newClientForm: {
        name: "",
        mobile: "",
        mobile2: "",
        password: "",
        sex: "",
        birthday: "",
        referrer: "",
        sourceId: "",
      },
      ClientSourceData: [],

      Sexoptions: [
        {
          value: 0,
          label: "未知",
        },
        {
          value: 1,
          label: "男",
        },
        {
          value: 2,
          label: "女",
        },
      ],

      options: [],

      newClientRules: {
        name: [{ required: true, message: "请输入客户姓名", trigger: "blur" }],
        mobile: [
          { required: true, message: "请输入手机号" },
          {
            validator: this.checkPhone,
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入6位数消费密码", trigger: "blur" },
          { min: 6, max: 6, message: "请输入6位数消费密码", trigger: "blur" },
        ],
        sex: [{ required: true, message: "请输入活动名称", trigger: "blur" }],
        sourceId: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
        ],
      },

      seachTimeout: null,
      storeUserLoading: false,
      userList: [],
    };
  },

  mounted() {},

  methods: {
    checkPhone(rule, value, callback) {
      if (!phoneTest(value) && !!value) {
        callback(new Error("请输入正确手机号格式"));
      } else {
        callback();
      }
    },
    open() {
      this.newClientdialogVisible = true;
      this.getClientSource();
      this.getUser();
    },

    getUser() {
      this.storeUserLoading = true;
      getStoreUser({})
        .then((res) => {
          console.log(res);
          this.storeUserLoading = false;
          const { code, data } = res;
          if (code === 0) {
            this.userList = data;
          }
        })
        .catch((err) => {
          this.storeUserLoading = false;
        });
    },

    getClientSource() {
      getClientSourceList().then((res) => {
        const { code, data } = res;
        if (code === 0) {
          this.ClientSourceData = data;
        }
      });
    },

    // 新增客户 弹框 确认
    ClientSubmit() {
      this.$refs["newClientForm"].validate((valid) => {
        if (valid) {
          this.NewClient();
        } else {
        }
      });
    },

    NewClient() {
      CreateClient({
        ...this.newClientForm,
      }).then((res) => {
        const { code } = res;
        if (code === 0) {
          this.CancelClientSubmit();
          this.$nextTick(() => {
            this.$emit("onSuccess");
          });
        }
      });
    },

    // 取消 弹框 关闭
    CancelClientSubmit() {
      this.resetForm("newClientForm");
      this.$nextTick(() => {
        this.newClientdialogVisible = false;
      });
    },

    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
</style>